package androidx.lifecycle

open class OnceLiveData<T> : MutableLiveData<T> {

    constructor() : super()

    constructor(value: T) : super(value) {
    }

    override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
        val pictorialObserver = PictorialObserver(observer, this.version > START_VERSION)
        super.observe(owner, pictorialObserver)
    }

    class PictorialObserver<T>(
        private val realObserver: Observer<in T>,
        private var preventDispatch: Boolean = false
    ) : Observer<T> {

        override fun onChanged(value: T) {
            // 如果版本有差异，第一次不处理
            if (preventDispatch) {
                preventDispatch = false
                return
            }
            realObserver.onChanged(value)
        }

    }
}